package com.lark.wifi.base;

import android.content.Context;
import android.net.ConnectivityManager;


import com.lark.wifi.util.MyLog;
import com.mtk.launcher.R;

import java.util.List;

public class WifiManager extends BaseWifiManager {

    private WifiManager(Context context) {
        super(context);
    }

    public static IWifiManager create(Context context) {
        return new WifiManager(context);
    }

    @Override
    public boolean isOpened() {
        return manager.isWifiEnabled();
    }

    @Override
    public void openWifi() {
        if (!manager.isWifiEnabled())
            manager.setWifiEnabled(true);
    }

    @Override
    public void closeWifi() {
        if (manager.isWifiEnabled())
            manager.setWifiEnabled(false);
    }

    @Override
    public void scanWifi() {
        manager.startScan();
    }

    @Override
    public boolean disConnectWifi() {
        return manager.disconnect();
    }

    @Override
    public boolean connectEncryptWifi(IWifi wifi, String password) {
        if (manager.getConnectionInfo() != null) {
            String ssid = wifi.SSID();
            String managerSsid = manager.getConnectionInfo().getSSID();
            MyLog.wifi("ssid=" + ssid + " /managerSsid=" + managerSsid);
            if (ssid.equals(managerSsid)) {
                return true;
            }
        }
        int networkId = WifiHelper.configOrCreateWifi(manager, wifi, password);
        MyLog.wifi("networkId=" + networkId);
        boolean ret = manager.enableNetwork(networkId, true);
        MyLog.wifi("ret=" + ret);
        modifyWifi(wifi.SSID(), "开始连接...");
        return ret;
    }

    @Override
    public boolean connectSavedWifi(IWifi wifi) {
        int networkId = WifiHelper.configOrCreateWifi(manager, wifi, null);
        boolean ret = manager.enableNetwork(networkId, true);
        modifyWifi(wifi.SSID(), "开始连接...");
        return ret;
    }

    @Override
    public boolean connectOpenWifi(IWifi wifi) {
        boolean ret = connectEncryptWifi(wifi, null);
        modifyWifi(wifi.SSID(), "开始连接...");
        return ret;
    }

    @Override
    public boolean removeWifi(IWifi wifi) {
        boolean ret = WifiHelper.deleteWifiConfiguration(manager, wifi);
        modifyWifi();
        return ret;
    }

    @Override
    public List<IWifi> getWifi() {
        return iWifiList;
    }


    public int getWifiImageShowByLevel(int wifiLevel) {
        if (wifiLevel >= -60) {
            return R.mipmap.wifilevel4_lock;
        }
        if (wifiLevel >= -70 && wifiLevel < -60) {
            return R.mipmap.wifilevel3_lock;
        }
        if (wifiLevel >= -80 && wifiLevel < -70) {
            return R.mipmap.wifilevel2_lock;
        }
        if (wifiLevel >= -90 && wifiLevel < -80) {
            return R.mipmap.wifilevel1_lock;
        }
        return R.mipmap.wifilevel1_lock;
    }

    public static String signalLevToStr(Context context, int level) {
        int value = Math.abs(level);
        if (value > 100) {
            return context.getString(R.string.not_signal);
        }
        if (value > 80) {
            return context.getString(R.string.str_weak);
        }
        if (value > 70) {
            return context.getString(R.string.str_commonly);
        }
        return context.getString(R.string.str_stronger);
    }


}
